package com.azt.front.action.user;

import com.azt.api.pojo.Address;
import com.azt.api.service.AddressService;
import com.azt.front.permission.RequireUser;
import com.azt.front.utils.ContextUtil;
import com.azt.model.AjaxJson;
import com.azt.utils.CommonUtil;
import com.xiaoleilu.hutool.convert.Convert;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 收货地址Action
 * Created by shangwq on 2016/10/13.
 */

@RequireUser
@Controller
@RequestMapping("/address")
public class AddressAction {

    private static final Logger LOGGER = Logger.getLogger(AddressAction.class);

    @Autowired
    private AddressService addressService;

    /**
     * 新增、编辑收货地址
     * */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxJson saveAddress(HttpServletRequest request, HttpServletResponse response, Address address) {
        if(address.getSetdefault() == null) {
            address.setSetdefault(0);
        }
        address.setUserId(ContextUtil.getUserId());

        if(address.getId() != null) {
            //更新
            addressService.updateAddress(address);
        } else {
            //新增
            addressService.addAddress(address);
        }
        return AjaxJson.success();
    }

    /**
     * 设置某一收货地址为默认的收货地址
     * */
    @ResponseBody
    @RequestMapping(value = "/setDeafault")
    public AjaxJson setDeafault(HttpServletRequest request) {
        Integer addressId = Convert.toInt(request.getParameter("addressId"));

        if(addressId != null) {
            addressService.setDefaultAddress(ContextUtil.getUserId(), addressId);
            return AjaxJson.success();
        }
        return AjaxJson.error("操作失败");
    }

    /**
     * 删除收货地址
     * */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public AjaxJson removeAddress(HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer addressId = CommonUtil.safeToInteger(request.getParameter("addressId"));

        if(addressId != null) {
            addressService.removeAddress(addressId);
            return AjaxJson.success();
        }
        return AjaxJson.error("操作失败");
    }

    @ResponseBody
    @RequestMapping(value = "/{addressId}", method = RequestMethod.POST)
    public AjaxJson getAddress(@PathVariable Integer addressId,  HttpServletRequest request) {
        if(addressId != null) {
            AjaxJson aj = AjaxJson.success();
            aj.setObj(addressService.getAddressById(addressId));
            return aj;
        }
        return AjaxJson.error("操作失败");
    }
}
